package cn.msn.basic.controller;

import cn.msn.basic.dto.SmsCodeDto;
import cn.msn.basic.dto.SmsEmailCodeDto;
import cn.msn.basic.exception.BusinessException;
import cn.msn.basic.service.IVerifyCodeService;
import cn.msn.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key")String key){
        try {
            //调用业务代码获取图形验证码（base64编码的字符串）
            String base64Str = verifyCodeService.image(key);
            //将（base64编码的字符串）响应给前端
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
            // 业务异常加(BusinessException
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "获取验证码失败");
        }
    }
    @PostMapping("/smsEmailCode")
    public AjaxResult smsEmailCode(@RequestBody SmsEmailCodeDto smsEmailCodeDto) {
        try {
            verifyCodeService.smsEmailCode(smsEmailCodeDto);
            return new AjaxResult();
            // 业务异常加(BusinessException
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取验证码失败");
        }
    }
    @PostMapping("/binderSmsCode")
    public AjaxResult binderCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            verifyCodeService.binderCode(smsCodeDto);
            return new AjaxResult();
            // 业务异常加(BusinessException
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "获取验证码失败");
        }
    }

}
